package com.sl.concurrence;

import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by lisulong on 2019/12/8.
 */
public class CAS1 {

    private AtomicReference<Thread> lockedBy = new AtomicReference<Thread>();

    public void lock() throws InterruptedException {
        Thread current = Thread.currentThread();
        while (!lockedBy.compareAndSet(null, current)) {

        }
    }

    public void unlock() {
        Thread current = Thread.currentThread();
        lockedBy.compareAndSet(current, null);
    }

}